package com.happyfamily.springboot.crowdsourcingplatform.controller.weng;

import com.google.gson.Gson;
import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.*;
import com.happyfamily.springboot.crowdsourcingplatform.service.pan.UserService;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.AllianceService;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.BankService;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.SysUserService;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.BackAllianceService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api-back/backAlliance")
@Controller
@Slf4j
public class BackAllianceController
{
	@Autowired
	BackAllianceService backAllianceService;
	@Autowired
	SysUserService sysUserService;
	@Autowired
	private AllianceService allianceService;
	@Autowired
	private BankService bankServiceImpl;


	@GetMapping("/list")
	@ResponseBody
	@OperationLogDetail(detail = "后台获取联盟信息", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public Results<Alliance> getAllAllianceByPage(PageTableRequest request)
	{
		request.countOffset();
		return backAllianceService.getAllAllianceByPage(request.getOffset(), request.getLimit());
	}

	@GetMapping("/findAllianceByFuzzy")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:alliance:query')")
	@ApiOperation(value = "后台模糊查询联盟信息", notes = "后台模糊查询联盟信息", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "后台模糊查询联盟信息", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public Results<Alliance> getAllianceByFuzzy(PageTableRequest requests, String aname, String sid)
	{
		requests.countOffset();
		return backAllianceService.getAllianceByFuzzy(aname, sid, requests.getOffset(), requests.getLimit());
	}


	@GetMapping("/deleteAlliance")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:alliance:delete')")
	@ApiOperation(value = "通过ID删除联盟信息", notes = "通过ID删除联盟信息", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "后台删除联盟信息", level = 4, operationUnit = OperationUnit.ADMIN, operationType = OperationType.DELETE)
	public Results<Alliance> deleteAlliance(String aid, String amargin, String amasterid)
	{
		SysUser sysUser = sysUserService.getUser(amasterid);
		boolean flag = allianceService.checkOrder(Long.valueOf(aid));
		if (flag)
		{
			List<User_alliance> list = allianceService.getUsers(Long.valueOf(aid));
			for (User_alliance ua : list)
			{
				if (!(sysUser.getUid().toString().equals(ua.getUid() + "")))
				{
					SysUser user = sysUserService.getUserById(ua.getUid());
					sysUserService.updateMoney(Integer.valueOf(user.getUmoney()) + Integer.valueOf(amargin) + "", ua.getUid());
				}
				bankServiceImpl.deleteBank(sysUser.getUaccount());
			}
			allianceService.deleteAlliance(Long.valueOf(aid));
			allianceService.deleteAll(Long.valueOf(aid));
		}
		return Results.success();
	}


	@RequestMapping("getUsers")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:alliance:query')")
	@ApiOperation(value = "通过ID获取联盟成员信息", notes = "通过ID获取联盟成员信息", httpMethod = "GET", response = String.class)
	@OperationLogDetail(detail = "获取联盟成员信息", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	public String getUsers(String page, String aid, HttpServletRequest request, String amasterid)
	{
		TableModel tableModel = new TableModel();
		tableModel.setCode(0);
		tableModel.setMsg("");
		List<User_alliance> list = allianceService.getUsers(Long.valueOf(aid));
		List<SysUser> users = new ArrayList<>();
		for (User_alliance ua : list)
		{
			if (ua.getUastate().equals("运行中"))
			{
				SysUser sysUser = sysUserService.getUserById(ua.getUid());
				if (!(sysUser.getUaccount().equals(amasterid)))
				{
					String type = allianceService.getServerType(Long.valueOf(sysUser.getSid()));
					sysUser.setTips1(type);
					users.add(sysUser);
				}
			}
		}
		tableModel.setCount(users.size());
		int p = Integer.valueOf(page);
		int n = 5 * (p - 1);
		List<SysUser> userList = new ArrayList<>();
		int sum = users.size() - 1;
		if (n + 4 <= users.size() - 1)
		{
			sum = n + 4;
		}
		for (int i = n; i <= sum; i++)
		{
			userList.add(users.get(i));
		}
		tableModel.setData(userList);
		;
		Gson g = new Gson();
		return g.toJson(tableModel);
	}
}
